<?php


namespace plugin\colewechat\app\service;


use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

class ServerService extends BaseService
{
    public function serve()
    {
        $request = request();
        $symfony_request = new SymfonyRequest($request->get(), $request->post(), [], $request->cookie(), [], [], $request->rawBody());
        $symfony_request->headers = new HeaderBag($request->header());
        $app = $this->getApp();
        $app->rebind('request', $symfony_request);
        $app->server->push(function ($message) {
            // $message['FromUserName'] // 用户的 openid
            // $message['MsgType'] // 消息类型：event, text....
            switch ($message['MsgType']) {
                case 'event':
                    return '收到事件消息' . $message['FromUserName'];
                case 'text':
                    return '收到文字消息' . $message['FromUserName'];
//                case 'image':
//                    return '收到图片消息';
                default:
                    return "您好！欢迎使用 coleWechat";
            }
        });

        $response = $app->server->serve();
        return $response->getContent();
    }
}